//
//  GZNewFeatureViewController.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/23.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit
private let reuseIdentifier = "Cell"
private let pageNumber = 4
class GZNewFeatureViewController: UICollectionViewController {
    let layout = UICollectionViewFlowLayout()
    override func viewDidLoad() {
        super.viewDidLoad()
        preparUI()
        self.collectionView!.registerClass(GZNewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    init(){
        layout.itemSize = UIScreen.mainScreen().bounds.size
        print("\(layout.itemSize)")
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        super.init(collectionViewLayout: layout)
        
    }
    private func preparUI(){
      
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension GZNewFeatureViewController{
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageNumber
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! GZNewFeatureViewCell
        cell.index = indexPath.item + 1
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
       let index = Int((collectionView?.contentOffset.x)! / UIScreen.mainScreen().bounds.width)
        if index == pageNumber-1{
            let indexpath = NSIndexPath(forItem: pageNumber-1, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexpath) as? GZNewFeatureViewCell
            cell?.startButtonAnimation()
            
        }
    }
}